#!/usr/bin/python
# -*- coding: UTF-8 -*-

from time import sleep
from MiniBotControlLib import MiniBotControlLib
from BwSensorLib import BwSensorLib

# 声明控制库对象
mlib = MiniBotControlLib("udp")
# 声明传感器库对象
sensorlib = BwSensorLib(mlib)
# 连接机器人
ip = "10.10.100.254"
mlib.Connect(ip, 8899)

# 点阵显示
# demo此处使用的5*7点阵作为演示
#  5*7点阵共有5行，因此需要创建长度为5的数组，每行用8位二进制的表示（因每行只有7个方格因此最高位不显示）;
#  demo中显示的效果为数字1，以1表示LED亮，0表示LED灭。
#  由于传输时先传低4位，后传高4位，故每一行转换为16进制数时应为下方displayData数组;
# 根据协议，赋5个16进制的数给unsigned char类型
# displayData = [[0x36,0x49,0x49,0x00,0x3E],[0x63,0x14,0x22,0x00,0x3E],[0x77,0x44,0x00,0x2A,0x14],
#             [0x41,0x36,0x00,0x1C,0x22],[0x22,0x77,0x22,0x1C,0x14],[0x20,0x12,0x20,0x08,0x1C]]


# 点阵显示0-99两次
def main():
    # 每个list对应显示为数字0～9
    numberData = [[0x07,0x05,0x05,0x05,0x07],[0x02,0x06,0x02,0x02,0x07],[0x07,0x01,0x07,0x04,0x07],
                [0x07,0x01,0x07,0x01,0x07],[0x05,0x05,0x07,0x01,0x01],[0x07,0x04,0x07,0x01,0x07],
                [0x07,0x04,0x07,0x05,0x07],[0x07,0x01,0x01,0x01,0x01],[0x07,0x05,0x07,0x05,0x07],
                [0x07,0x05,0x07,0x01,0x07]]

    # 输入到点阵显示参数
    displayData = [0] * 5

    for count in range (0,2):
        for a in range (0,10): # 十位数确定（点阵靠左侧数字）
            for b in range (0,10): # 个位数确定（点阵靠右侧数字）
                for c in range (0,5): # 对每个list的元素进行操作
                    displayData[c] = (numberData[a][c] << 4) + numberData[b][c] # 先个位数左移4位，形成十位数，再补齐个位数
                sensorlib.setMatrixSensor(displayData) # 点阵显示数字
                sleep(0.5)                        


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print(" Program closed! ")
    pass
